package ar.uba.tleng.g99.expregs.automata;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

class NodeCopier {

	private final Map<Node, Node> t = new HashMap<Node, Node>();
	
	public NodeCopier(Node initialNode) {
		doTranslate(initialNode);
	}
	
	private Node doTranslate(Node n) {
		if (t.containsKey(n)) {
			return t.get(n);
		}
		Node n2 = new Node();
		t.put(n, n2);
		for (Entry<Character, Set<Node> > e : n.childs.entrySet()) {
			for (Node d : e.getValue()) {
				n2.addTransition(e.getKey(), doTranslate(d));
			}
		}
		return n2;
	}
	
	public Set<Node> getAllKeys() {
		return Collections.unmodifiableSet(t.keySet());
	}

	public Node translate(Node node) {
		return t.get(node);
	}
	
	public int translateSize() {
		return t.size();
	}

	public Set<Node> translateMany(Set<Node> nodes) {
		Set<Node> result = new HashSet<Node>();
		for (Node n : nodes) {
			result.add(translate(n));
		}
		return result;
	}

}
